package com.csii.mobilebanking.user;

import com.csii.mobilebanking.R;
import com.csii.mobilebanking.tool.HpConstants;
import com.csii.mobilebanking.tool.HpGlobals;
import com.csii.mobilebanking.tool.HpUtils;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class UserRegister {
	public Context mContext;
	public RelativeLayout view;
	private EditText mRegisterNumber;
	private EditText mRegisterPassword;
	private EditText mRegisterPassword2;
	private Button mRegisterButton;
	
	private CommUser mXGUser = new CommUser();

	public UserRegister(Context aContext){
		mContext = aContext;
		initView();
	}
	
	private void initView()
	{
		view = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.hwzj_user_register, null);
		mRegisterNumber = (EditText)view.findViewById(R.id.register_num);
		mRegisterPassword = (EditText)view.findViewById(R.id.register_password);
		mRegisterPassword2 = (EditText)view.findViewById(R.id.register_password_reinput);
		mRegisterButton = (Button)view.findViewById(R.id.register);
		mRegisterButton.setOnClickListener(registerClick);
		mRegisterNumber.setOnClickListener(showDialogClick);
	}
	
	private String getError(){
		String registerNumber = mRegisterNumber.getText().toString();
		if(registerNumber == null || registerNumber.equals(""))
        {
			mRegisterNumber.requestFocus();
            return "请输入手机号码";
        }
        if(registerNumber.length() != 11)
        {
        	mRegisterNumber.requestFocus();
            return "请输入11位手机号码";
            
        }
        if(!registerNumber.matches("\\d{11}"))
        {
        	mRegisterNumber.requestFocus();
            return "您只能输入数字";
        }
        if(!registerNumber.matches("^(1[0-9]*)$"))
        {
        	mRegisterNumber.requestFocus();
            return "你写的手机号码不符合规范，请重写过！谢谢！";
        }
        
        String registerPassword = mRegisterPassword.getText().toString();
        if((registerPassword == null) || (registerPassword.equals("")))
        {
        	mRegisterPassword.requestFocus();
            return "请输入密码";
        }
        if(registerPassword.length() < 6 || registerPassword.length() > 15)
        {
        	mRegisterPassword.requestFocus();
            return "请输入6-15位字符";
        }
       
   
        
        String registerPassword2 = mRegisterPassword2.getText().toString();
        if(!registerPassword.equals(registerPassword2))
        {
        	mRegisterPassword2.requestFocus();
            return "两次输入的密码不一致,请重新输入";
        }
		return "";
	}
	
	//注册
	private OnClickListener registerClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String error = getError();
			if (!error.equals("")){
				HpToast.showMessageBottom(mContext, error);
			}
			else{
				mXGUser.Phone = mRegisterNumber.getText().toString();
				mXGUser.Password = mRegisterPassword.getText().toString(); 
				mXGUser.AppVersion = HpGlobals.AppVer;
				new RegisterTask().execute("");
			}
		}
	};
	
	//点击手机号码输入框时弹出提示信息
	private OnClickListener showDialogClick = new OnClickListener(){
		@Override
		public void onClick(View v) {
			String aMsg="手机号码在大奖通知以及找回密码功能中将会采用";
			HpToast.showMessageBottom(mContext,aMsg);


		}
	};
	
	class RegisterTask extends AsyncTask<String, Integer, Object>
	{
		
		@Override  
		protected void onPreExecute() 
		{  
			super.onPreExecute();  
			HpDialog.show(mContext, HpConstants.MsgRegisterProcess);
		} 
		
		@Override    	
		protected void onPostExecute(Object result) { 
			HpDialog.close();
			TResponse resp = (TResponse)result;
			if (HpUtils.isOk(resp)){
				((Activity)mContext).finish();
			}
			else{
				String memo = HpUtils.getMemo(resp);
				if (memo.equals("")){
					HpToast.showMessageBottom(mContext, HpConstants.MsgRegisterError + ",请检查网络连接!");
				}
				else{
					HpToast.showMessageBottom(mContext, HpConstants.MsgRegisterError + memo);
				}
			}
			super.onPostExecute(result);          
		}  

		@Override
		protected Object doInBackground(String... params) {
//			return AppService.Registe(mXGUser);
			return params;
		}		
	}

}
